package com.yifeixi.validation.controller;

import com.yifeixi.validation.entity.User;
import com.yifeixi.validation.group.IdGroup;
import com.yifeixi.validation.util.ValidateUtil;
import javax.validation.Validator;
import javax.validation.groups.Default;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户管理
 *
 * @author YiFeiXi
 */
@RestController
@AllArgsConstructor
public class UserController {

  private Validator validate;

  static final String REQUEST_BODY_MAPPING_PATH = "/user";

  @PostMapping(REQUEST_BODY_MAPPING_PATH)
  public boolean save(@RequestBody User user) {
    ValidateUtil.validate(validate, user);
    return true;
  }

  @PutMapping(REQUEST_BODY_MAPPING_PATH)
  public boolean update(@RequestBody User user) {
    ValidateUtil.validate(validate, user, Default.class, IdGroup.class);
    return true;
  }

  @DeleteMapping(REQUEST_BODY_MAPPING_PATH)
  public boolean delete(@RequestBody User user) {
    ValidateUtil.validate(validate, user, IdGroup.class);
    return true;
  }
}
